﻿/********************************************/
// 作者：米海波
// 时间：$time
// 公司: 一网金
// 版权：2011-2012  
// CLR版本：4.0.30319.225  
// 公司地址：http://www.cnproter.com
// DialogFormEx说明：  
// 唯一标识：f37727fd-9f14-42aa-8389-481ce8c5a72a  
/********************************************/

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using Hmc.IM.Common.UserControls;
using Hmc.IM.Common.Base;
namespace Hmc.IM.Common.Forms
{
    /// <summary>
    /// Dialog窗体（扩展），支持自定义放大
    /// </summary>
    public partial class DialogFormEx : Form, IBaseForm
    {
        private const int WM_NCLBUTTONDOWN = 0xA1;
        private const int HT_CAPTION = 0x2;

        /// <summary>
        /// 系统API函数，发送消息
        /// </summary>
        /// <param name="hWnd"></param>
        /// <param name="Msg"></param>
        /// <param name="wParam"></param>
        /// <param name="lParam"></param>
        /// <returns></returns>
        [DllImportAttribute("user32.dll")]
        public static extern int SendMessage(IntPtr hWnd, int Msg, int wParam, int lParam);
        /// <summary>
        /// 系统API函数，移动窗体
        /// </summary>
        /// <returns></returns>
        [DllImportAttribute("user32.dll")]
        public static extern bool ReleaseCapture();

        /// <summary>
        /// 标题
        /// </summary>
        public string TitleText
        {
            get
            {
                return lbTitle.Text;
            }
            set
            {
                lbTitle.Text = value;
            }
        }

        /// <summary>
        /// 构造函数
        /// </summary>
        public DialogFormEx()
        {
            InitializeComponent();
            this.ImeMode = System.Windows.Forms.ImeMode.KatakanaHalf;
            //this.TransparencyKey = this.BackColor;
            plTop.MouseDown += new MouseEventHandler(pTitle_MouseDown);
            lbTitle.MouseDown += new MouseEventHandler(pTitle_MouseDown);
            this.Width = base.Width;
            this.Height = base.Height;
        }

        /// <summary>
        /// 权限ID
        /// </summary>
        private int _powerId = -1;

        /// <summary>
        /// 权限ID
        /// </summary>
        public int PowerId
        {
            get
            {
                return _powerId;
            }
            set
            {
                _powerId = value;
            }
        }

        /// <summary>
        /// 是否已经打开一次
        /// </summary>
        private bool isPower = false;
        /// <summary>
        /// 重写OnVisibleChanged
        /// </summary>
        /// <param name="e"></param>
        protected override void OnVisibleChanged(EventArgs e)
        {
            base.OnVisibleChanged(e);
            if (DesignMode) return;
            if (PowerId == -1)
            {
                this.Close();
                //已经打开一次
                if (isPower) return;
                isPower = true;
                Exception ex = new Exception("请对权限ID进行赋值!");
                ex.Source = this.Name;
                throw ex;
            }
            if (!this.IsRight(PowerId) && PowerId != 0)
            {
                //已经打开一次
                if (isPower) return;
                MessageBox.Show("无该模块查看权限,请联系负责人！", "消息", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
                isPower = true;
                this.Close();
            }
        }

        /// <summary>
        /// 根据权限点判断是否拥有权限
        /// </summary>
        /// <param name="rightID"></param>
        /// <returns></returns>
        public virtual bool IsRight(int rightID)
        {
            var query = from p in LoginUserEntity.PermissionsPoint where p == rightID select p;
            if (query.Count() > 0)
            {
                return true;
            }
            return false;
        }

        /// <summary>
        /// 鼠标点击移动窗体
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void pTitle_MouseDown(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                ReleaseCapture();
                SendMessage(Handle, WM_NCLBUTTONDOWN, HT_CAPTION, 0);
            }
        }

        /// <summary>
        /// 窗体最大化
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void pbMax_Click(object sender, EventArgs e)
        {
            if (this.WindowState == FormWindowState.Normal)
            {
                this.MaximumSize = new Size(Screen.PrimaryScreen.WorkingArea.Width, Screen.PrimaryScreen.WorkingArea.Height);
                this.WindowState = FormWindowState.Maximized;
            }
            else
            {
                this.WindowState = FormWindowState.Normal;
            }
        }

        /// <summary>
        /// 鼠标进入关闭按钮控件范围
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void pbClose_MouseEnter(object sender, EventArgs e)
        {
            pbClose.BackgroundImage = Properties.Resources.close_;
        }

        /// <summary>
        /// 鼠标离开关闭按钮范围
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void pbClose_MouseLeave(object sender, EventArgs e)
        {
            pbClose.BackgroundImage = Properties.Resources.close;
        }

        /// <summary>
        /// 关闭窗体
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void pbClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        /// <summary>
        /// 鼠标进入最大化按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void pbMax_MouseEnter(object sender, EventArgs e)
        {
            pbMax.BackgroundImage = Properties.Resources.max;
        }

        /// <summary>
        /// 鼠标离开最大化按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void pbMax_MouseLeave(object sender, EventArgs e)
        {
            pbMax.BackgroundImage = Properties.Resources.upMax;
        }

        /// <summary>
        /// 验证指定集合控件下的数据格式
        /// </summary>
        /// <param name="controls">需验证的数据集合</param>
        /// <returns></returns>
        public bool CheckValidate(Control controls)
        {
            bool fay = true;
            foreach (Control ctl in controls.Controls)
            {
                if (ctl is IValidate)
                {
                    IValidate box = ctl as IValidate;
                    if (!box.Validate())
                    {
                        fay = false;
                    }
                }
                else if (ctl.Controls.Count > 0)
                {
                    CheckValidate(ctl);
                }
            }
            return fay;
        }

        /// <summary>
        /// 验证当前窗体集合下的数据格式
        /// </summary>
        /// <returns></returns>
        public bool CheckValidate()
        {
            return CheckValidate(this);
        }
    }
}
